package com.platform.qujia.controller;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.platform.qujia.pojo.FleetCardSubForm;
import com.platform.qujia.service.FleetCardSubService;
import com.platform.qujia.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin
@Api(tags = "车队子卡信息管理")
@RequestMapping("/fleetCardSub")
public class FleetCardSubController {

    @Autowired
    private FleetCardSubService fleetCardSubService;

    @GetMapping(value = "/enterpriseId/{enterpriseId}/userId/{userId}")
    @ApiOperation(value = "根据用户id获取车队子卡信息", httpMethod = "GET", notes = "根据用户id获取车队子卡信息")
    public String getFleetCardSub(@PathVariable Long enterpriseId, @PathVariable Integer userId) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(ResultData.success(fleetCardSubService.selectOneFleetCardSub(enterpriseId, userId)));
    }

    @PostMapping(value = "/apply")
    @ApiOperation(value = "申请加入主卡", httpMethod = "POST", notes = "申请加入主卡")
    public String apply(@Valid @RequestBody FleetCardSubForm fleetCardSubForm) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(ResultData.success(fleetCardSubService.apply(fleetCardSubForm)));
    }


    @DeleteMapping(value = "/unbind/{id}")
    @ApiOperation(value = "解除绑定", httpMethod = "DELETE", notes = "解除绑定")
    @ApiImplicitParam(paramType = "path", name = "id", value = "子卡id", required = true)
    public String unbind(@PathVariable Long id) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(ResultData.success(fleetCardSubService.unbind(id)));
    }
}
